package day02;

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

public class TreeSetDemo {
    public static void main(String[] args) {
        //集合只可以存引用数据类型
        TreeSet<Integer> set1 = new TreeSet<>();
        //1.自动包装成integer对象（自动装箱）
        //自动排序
        set1.add(1);
        set1.add(2);
        set1.add(5);
        set1.add(3);
        set1.add(4);

        System.out.println(set1);//输出1，2，3，4，5

        //for
        for(Integer i : set1) {
            System.out.println(i);
        }
//        test1();
        test2();
    }


    public static  void test1(){
        TreeSet<Student> set = new TreeSet<>();
        set.add(new Student("引用",18));
        set.add(new Student("wy用",28));
        set.add(new Student("o用",8));

        for(Student student : set) {
            System.out.println(student.getName()+"---"+student.getAge());
        }
    }

    public static  void test2(){
        //匿名内部类对象
//        Comparator<Student> comparator = new Comparator<Student>() {
//            @Override
//            public int compare(Student o1, Student o2) {
//                //如果正序排，则o1-o2
//                return o1.getAge()-o2.getAge();
//            }
//        };
        Comparator<Student> comparator = (o1,o2) -> o1.getAge() - o2.getAge();
        TreeSet<Student> set = new TreeSet<>(comparator);
        set.add(new Student("吴用",18));
        set.add(new Student("张顺",28));
        set.add(new Student("鲁智深",8));

        for(Student student : set) {
            System.out.println(student.getName()+"---"+student.getAge());
        }
    }
    //可变参数求和(可变参数必须是形参的最后一个)
    public static  int sum(int... nums){
        int total = 0;
        for (int i = 0; i < nums.length; i++) {
                total = total + nums[i];
        }
        return total;
    }
}
